// pages/venues/venues.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ThreeSubTitle: [],
    nNode:[],
    nodeActive: 0,
    active: 0,
    isSubTitleShow: true,
    loading: true,
    error: false,
    cacheConfig: {
      key: 'threeSubTitleData',
      expire: 3600
    },
  },

  /** 生命周期函数--监听页面加载 */
  onLoad(options) {
    this.loadThreeSubTitle()
  },

  /** 数据加载方法 */
  async loadThreeSubTitle() {
    try {
      const cacheData = await this.getCache()
      if (cacheData) {
        this.setData({ 
          ThreeSubTitle: cacheData.threeBase,
          nNode: cacheData.nNode,
          loading: false 
        })
        return
      }
  
      const res = await wx.cloud.callFunction({
        name: 'getVenuesList',
        config: { env: this.data.envId }
      })
      if (res.result.code === 200) {
        // 更新数据时同时保存两个数据集
        this.setData({
          ThreeSubTitle: res.result.threeBase,
          nNode: res.result.nNode,
          loading: false
        })
        // 缓存完整数据结构
        this.setCache({
          threeBase: res.result.threeBase,
          nNode: res.result.nNode
        })
      }
    } catch (e) {
      const cacheData = await this.getCache()
      if (cacheData) {
        this.setData({
          ThreeSubTitle: cacheData.threeBase,
          nNode: cacheData.nNode,  // 添加nNode设置
          loading: false
        })
      }
    }
  },

  /** 缓存操作 */
  getCache() {
    return new Promise((resolve) => {
      wx.getStorage({
        key: this.data.cacheConfig.key,
        success: (res) => {
          if (Date.now() - res.data.timestamp < this.data.cacheConfig.expire * 1000) {
            console.log('[缓存] 读取有效缓存数据')
            resolve(res.data.content)
          } else {
            console.log('[缓存] 缓存已过期')
            resolve(null)
          }
        },
        fail: (err) => {
          console.log('[缓存] 未找到缓存数据:', err.errMsg)
          resolve(null)
        }
      })
    })
  },

  setCache(data) {
    wx.setStorage({
      key: this.data.cacheConfig.key,
      data: {
        timestamp: Date.now(),
        content: data
      },
      success: () => {
        console.log('[缓存] 缓存写入成功，键名:', this.data.cacheConfig.key)
      },
      fail: (err) => {
        console.error('[缓存] 缓存写入失败:', err.errMsg)
      }
    })
  },

  clearCache() {
    wx.removeStorage({
      key: this.data.cacheConfig.key,
      success: () => console.log('[缓存] 缓存清除成功'),
      fail: (err) => console.error('[缓存] 缓存清除失败:', err.errMsg)
    })
  },

  /** 下拉刷新 */
  onPullDownRefresh() {
    console.log('[交互] 触发下拉刷新操作')
    this.clearCache()
    this.loadThreeSubTitle().finally(() => {
      wx.stopPullDownRefresh()
      console.log('111');
    })
  },

  /** 错误处理 */
  handleError() {
    console.error('[错误] 进入错误处理流程')
    this.setData({ loading: false, error: true })
    wx.showToast({ title: '数据加载失败', icon: 'none' })
  },

  /** 交互事件 */
  onVenues(e) {
    console.log('[交互] 点击三基地标签')
    this.setData({ isSubTitleShow: true, active: 0 })
  },

  onTabChange(e) {
    console.log('[交互] 切换基地标签，索引:', e.detail.index)
    this.setData({ active: e.detail.index })
  },

  onNode(e) {
    console.log('[交互] 点击节点标签')
    this.setData({ isSubTitleShow: false, nodeActive: 0 })
  },

  onTabNodeChange(e) {
    console.log('[交互] 切换节点标签，索引:', e.detail.index)
    this.setData({ nodeActive: e.detail.index })
  },

  // 其他生命周期函数保持原有空实现
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onReachBottom() {},
  onShareAppMessage() {}
})